package com.hu.zhcc.shiro.service.xy;

import com.google.gson.internal.$Gson$Preconditions;
import com.hu.zhcc.common.entity.Page;
import com.hu.zhcc.common.entity.PageRequest;
import com.hu.zhcc.shiro.dao.junM.TInterposeClassifyMapper;
import com.hu.zhcc.shiro.dao.lgx.TBodyCkMapper;
import com.hu.zhcc.shiro.dao.lgx.TMemberInfoMapper;
import com.hu.zhcc.shiro.dao.lgx.TMemberMapper;
import com.hu.zhcc.shiro.dao.xy.TAssessAnswerMapper;
import com.hu.zhcc.shiro.dao.xy.TAssessMapper;
import com.hu.zhcc.shiro.dao.xy.TAssessQuestionOptionMapper;
import com.hu.zhcc.shiro.entity.junM.pojo.TInterposeClassify;
import com.hu.zhcc.shiro.entity.lgx.TMember;
import com.hu.zhcc.shiro.entity.lgx.TMemberInfo;
import com.hu.zhcc.shiro.entity.xsf.pojo.TDisease;
import com.hu.zhcc.shiro.entity.xsf.pojo.TDiseaseLibrary;
import com.hu.zhcc.shiro.entity.xy.dto.TAssessDTO;
import com.hu.zhcc.shiro.entity.xy.pojo.TAssess;
import com.hu.zhcc.shiro.entity.xy.pojo.TAssessAnswer;
import com.hu.zhcc.shiro.entity.xy.pojo.TAssessQuestion;
import com.hu.zhcc.shiro.entity.xy.pojo.TBodyType;
import com.hu.zhcc.shiro.service.xsf.TDiseaseLibraryService;
import com.hu.zhcc.shiro.service.xsf.TDiseaseService;
import com.hu.zhcc.shiro.service.xsf.impl.TMealsImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class TAssessImpl implements TAssessService{

    @Autowired
    private TAssessMapper mapper;
    @Autowired
    private TMemberMapper tMemberMapper;
    @Autowired
    private TAssessAnswerMapper tAssessAnswerMapper;
    @Autowired
    private TDiseaseService  tDiseaseService;
    @Autowired
    private TDiseaseLibraryService  tDiseaseLibraryService;
    @Autowired
    private TAssessQuestionOptionMapper tAssessQuestionOptionMapper;
    @Autowired
    private TBodyCkMapper tBodyCkMapper;
    @Autowired
    private TMemberInfoMapper tMemberInfoMapper;
    @Autowired
    private TInterposeClassifyMapper tInterposeClassifyMapper;
    @Override
    public int deleteByPrimaryKey(Integer id) {
        return 0;
    }

    @Override
    public int insertSelective(TAssess record) {
        return mapper.insertSelective(record);
    }

    @Override
    public int insert(TAssess record) {
        return 0;
    }


    @Override
    public TAssess selectByPrimaryKey(Integer id) {
        return null;
    }

    @Override
    public int updateByPrimaryKeySelective(TAssess record) {
        return mapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(TAssess record) {
        return 0;
    }

    @Override
    public Page<TAssessDTO> listTAssess(Map<String, Object> paremeters, int offset, int limit) {
        int total = this.mapper.countTAssess(paremeters);
        List<TAssessDTO> dtoList = null;
        if(total > 0) {
            PageRequest pageRequest = new PageRequest(offset, limit, paremeters, null);
            List<TAssess> list = this.mapper.listTAssess(pageRequest);
            dtoList = new ArrayList<TAssessDTO>(list.size());
            for(TAssess type : list) {
                TAssessDTO dto = new TAssessDTO();
                BeanUtils.copyProperties(type, dto);
                dtoList.add(dto);
            }
        }
        return new Page<TAssessDTO>(total, dtoList);
    }

    @Override
    public TAssess selectMember(Integer memberId) {
        return mapper.selectTMember(memberId);
    }

    @Override
    public Integer updateTAssess(Integer assessId, TAssessQuestion[][] objects) {
      /*  TBodyType tBodyType = mapper.selectByPrimaryKey(memberId);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        tBodyType.setUpdatetime(simpleDateFormat.format(new Date()));
*/
//        mapper.selectByPrimaryKey()

        for(int i=0;i<objects.length;i++){
            Integer markTotal=0;

            TAssessQuestion[] tAssessQuestions=(TAssessQuestion[])objects[i];
            for(int j=0;j<tAssessQuestions.length;j++){
//                System.out.println(tAssessQuestions[j]);
                TAssessQuestion tAssessQuestion = tAssessQuestions[j];
                TAssessAnswer tAssessAnswer = tAssessAnswerMapper.selectByPrimaryKey(tAssessQuestion.getAnswer_id());
                //设置答案id
                if(tAssessQuestion.getIsRadio().equals("0")){
                    tAssessAnswer.setAnswerId(tAssessQuestion.getAnswers());
//                System.out.println(tAssessQuestionOptionMapper.selectByPrimaryKey(tAssessQuestion.getSelectOption()).getMark());
                    markTotal+=tAssessQuestionOptionMapper.selectByPrimaryKey(Integer.parseInt(tAssessQuestion.getAnswers())).getMark();
                }else{
                    String options="";
                    for(int k=0;k<tAssessQuestion.getCheckOptions().size();k++){
                        options+=tAssessQuestion.getCheckOptions().get(k);
                        Double mark = Double.parseDouble(tAssessQuestion.getCheckOptions().get(k).toString());
//                    System.out.println(tAssessQuestionOptionMapper.selectByPrimaryKey(mark.intValue()).getMark());
                        markTotal+=tAssessQuestionOptionMapper.selectByPrimaryKey(mark.intValue()).getMark();
                        if(k<tAssessQuestion.getCheckOptions().size()-1){
                            options+=",";
                        }
                    }
                    tAssessAnswer.setAnswerId(options);
                }
//                System.out.println(tAssessAnswer);
                tAssessAnswerMapper.updateByPrimaryKeySelective(tAssessAnswer);
            }

        }

        //新增人群分类表
        TInterposeClassify tInterposeClassify = new TInterposeClassify();
        TAssess tAssess = mapper.selectByPrimaryKey(assessId);
        //状态设置为2，表示评估完成
        tAssess.setState(2);
        mapper.updateByPrimaryKeySelective(tAssess);

        tInterposeClassify.setTMemberId(tAssess.getMemberId());
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd hh-mm-ss");
        tInterposeClassify.setCreatetime(simpleDateFormat.format(new Date()));
        //未分类
        tInterposeClassify.setStatus(0);
        //设置默认疾病
        List<TDisease> tDiseases = tDiseaseService.queryDiseaseByMemberId1(tAssess.getMemberId());
        String disease="";
        for(int i=0;i<tDiseases.size();i++){
            TDiseaseLibrary tDiseaseLibrary = tDiseaseLibraryService.selectByPrimaryKey(tDiseases.get(i).getDiseaseLibraryLib());
            disease+=tDiseaseLibrary.getName();
            if(i<tDiseases.size()-1){
                disease+=",";
            }
        }
        tInterposeClassify.setPeopleClassify(disease);
        //健康师id
        TMemberInfo tMemberInfo = tMemberInfoMapper.selectByMemberId(tAssess.getMemberId());
        Integer healthMgrId = tMemberInfo.getHealthMgrId();
        tInterposeClassify.setHealthmgrId(healthMgrId);

//        System.out.println(tBodyType);
        return tInterposeClassifyMapper.insertSelective(tInterposeClassify);
    }

    @Override
    public Integer addTAssess(String idCard, TAssessQuestion[][] objects) {
        TMember tMember = tMemberMapper.selectByIdCard(idCard);
        /*TAssess tAssess = new TAssess();
        tAssess.setMemberId(tMember.getId());
        Integer count = tBodyCkMapper.queryCountByMemberId(tMember.getId());
        if(count>0){
            //大于0代表体检结果已经上传了
            tAssess.setState(1);
        }else{
            tAssess.setState(0);
        }
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd hh-mm-ss");
        tAssess.setTime(simpleDateFormat.format(new Date()));
        int rows = mapper.insertSelective(tAssess);*/

        for(int i=0;i<objects.length;i++){
            Integer markTotal=0;

            TAssessQuestion[] tAssessQuestions=(TAssessQuestion[])objects[i];
            for(int j=0;j<tAssessQuestions.length;j++){
//                System.out.println(tAssessQuestions[j]);
                TAssessQuestion tAssessQuestion = tAssessQuestions[j];
                tAssessAnswerMapper.deleteByPrimaryKey(tAssessQuestion.getAnswer_id());
                TAssessAnswer tAssessAnswer = new TAssessAnswer();
                tAssessAnswer.setUserId(tMember.getId());
                tAssessAnswer.setQuestionId(tAssessQuestion.getId());

                //设置答案id
                if(tAssessQuestion.getIsRadio().equals("0")){
                    tAssessAnswer.setAnswerId(tAssessQuestion.getAnswers());
//                System.out.println(tAssessQuestionOptionMapper.selectByPrimaryKey(tAssessQuestion.getSelectOption()).getMark());
                    markTotal+=tAssessQuestionOptionMapper.selectByPrimaryKey(Integer.parseInt(tAssessQuestion.getAnswers())).getMark();
                }else{
                    String options="";
                    for(int k=0;k<tAssessQuestion.getCheckOptions().size();k++){
                        options+=tAssessQuestion.getCheckOptions().get(k);
                        Double mark = Double.parseDouble(tAssessQuestion.getCheckOptions().get(k).toString());
//                    System.out.println(tAssessQuestionOptionMapper.selectByPrimaryKey(mark.intValue()).getMark());
                        markTotal+=tAssessQuestionOptionMapper.selectByPrimaryKey(mark.intValue()).getMark();
                        if(k<tAssessQuestion.getCheckOptions().size()-1){
                            options+=",";
                        }
                    }
                    tAssessAnswer.setAnswerId(options);
                }
//                System.out.println(tAssessAnswer);
                tAssessAnswerMapper.insertSelective(tAssessAnswer);
            }

        }
//        System.out.println(tBodyType);
        return 1;
    }

}
